package gui;


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Librarian;
import logic.Message;
import logic.Reader;



/**
 * Shows a login form and delivers user's input.
 */
public class LoginForm extends JPanel implements ActionListener{
	
	private JLabel userNameLabel;
	private JLabel passwordLabel;
	private JTextField userNameTextField;
	private JPasswordField passwordTextField;
	private JLabel confirmationLable;
	private JButton loginbutton,back;
	/** Saves reader's instance that performed successful login */
	private Reader currReader;
	/** the main panel surrounding all GUI elements*/
	private JPanel panel;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	
	/**Default constructor- Builds the Login form*/
	public LoginForm() {
		super();
		setLayout(new BorderLayout());
		panel=new JPanel();
		
		JPanel backPanel=new JPanel();
		panel.setLayout(new GridBagLayout());
		gbc.insets= new Insets(7,7,7,7);
		userNameLabel = new JLabel("Username:");
		userNameTextField=new JTextField(15);
		passwordLabel = new JLabel("Password:");
		passwordTextField=new JPasswordField(15);
		loginbutton=	new JButton("Login");
		back=	new JButton("Back");
		loginbutton.addActionListener(this);
		
		
		confirmationLable=new JLabel();
		confirmationLable.setVisible(false);
		gbc.gridx=1;
		gbc.gridy=2;
		panel.add(confirmationLable,gbc);
		
		back.addActionListener(this);
		gbc.gridx=0;
		gbc.gridy=0;
		panel.add(userNameLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=0;
		panel.add(userNameTextField,gbc);
		gbc.gridx=0;
		gbc.gridy=1;
		panel.add(passwordLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=1;
		panel.add(passwordTextField,gbc);
		gbc.gridx=0;
		gbc.gridy=2;
		panel.add(loginbutton,gbc);
		add(panel,BorderLayout.CENTER);
		backPanel.add(Box.createHorizontalGlue());
		backPanel.add(back);
		add(backPanel,BorderLayout.SOUTH);
	
}
	
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if( ae.getSource() == loginbutton )
		{
			Message m;
			Reader r=new Reader();
			r.setUsername(userNameTextField.getText());
			r.setPassword(passwordTextField.getText());
			m=new Message("login",r);
			confirmationLable.setText("");
			
			if(userNameTextField.getText().isEmpty() || passwordTextField.getText().isEmpty())
			{
				 confirmationLable.setVisible(true);
				 confirmationLable.setText("Please fill all the fields!!");
			}
			else{
				ClientConsole.HandleMessageFromGui(m);
				
				m=(Message)ClientConsole.client.getMessage();
				
				if(m.getCommand().equals("User Connected"))	
				{
					this.setVisible(false);
					currReader=(Reader)m.getObjects();
					ClientConsole.reader=currReader;
					
					if (m.getObjects() instanceof LibraryManager)
						ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());	
					else if (m.getObjects() instanceof Librarian)
						ClientConsole.aFrame.setContentPane(new LibrarianWindow());	
					else
						ClientConsole.aFrame.setContentPane(new ReaderWindow());	
					
				}
				else{
					System.out.println("return masage:"+m.getCommand());
					confirmationLable.setVisible(true);
					confirmationLable.setText(m.getCommand());
				}
			}
		
		}
		else if ( ae.getSource() == back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new GuestWindow());
		}
	}
}

